<?php
if (!(defined('IN_IA'))) 
{
	exit('Access Denied');
}
class Index_Page extends WebPage
{

	public function bottom() 
	{
        global $_W,$_GPC;
        $uniacid=$_W['uniacid'];
        load()->func('tpl');
        $result=pdo_fetch("SELECT * FROM ".tablename('hs_cars_tabbar')." where `uniacid`='$uniacid' order by id desc limit 1");
        //var_dump($_GPC);exit;
        if($_W['ispost']){
            //echo $_GPC['m3_name'];
            $data=array();
            $data['iconsize']=$_GPC['iconsize'];
            $data['m1_name']=$_GPC['m1_name'];
            $data['m2_name']=$_GPC['m2_name'];
            $data['m3_name']=$_GPC['m3_name'];
            $data['m4_name']=$_GPC['m4_name'];
            $data['m1_img']=$_GPC['m1_img'];
            $data['m2_img']=$_GPC['m2_img'];
            $data['m3_img']=$_GPC['m3_img'];
            $data['m4_img']=$_GPC['m4_img'];
            $data['m1_selimg']=$_GPC['m1_selimg'];
            $data['m2_selimg']=$_GPC['m2_selimg'];
            $data['m3_selimg']=$_GPC['m3_selimg'];
            $data['m4_selimg']=$_GPC['m4_selimg'];
            $data['background_color']=$_GPC['background_color'];
            $data['color']=$_GPC['color'];
            $data['sel_color']=$_GPC['sel_color'];
            $data['uniacid']=$_W['uniacid'];
            $data['on_off']=1;
            //echo $data['m3_name'];exit;
            $result=pdo_fetch("SELECT `id` FROM ".tablename('hs_cars_tabbar')." where `uniacid`='$uniacid' order by id desc limit 1");
            if($result['id']){
                $res=pdo_update('hs_cars_tabbar',$data,array('id'=>$result['id']));
            }else{
                $res=pdo_insert('hs_cars_tabbar',$data);
            }
            $this->success('修改成功','other/index/bottom');
        }
        include $this->template();
	}
    /*
    * 开启关闭自定义底部导航
     */
    public function asdsa()
    {
        global $_W,$_GPC;
        $uniacid=$_W['uniacid'];
        $date=array();
        $data['on_off']=$_GPC['status'];
        $res=pdo_update('hs_cars_tabbar',$data,array('uniacid'=>$uniacid));
        echo 1;
    }
    public function modules()
    {
        global $_W,$_GPC;
        $uniacid=$_W['uniacid'];
        load()->func('tpl');
        $modules_config = include_once IA_ROOT . '/addons/'.MODEL_NAME.'/config/modules.php';
        $result=pdo_fetch("SELECT * FROM ".tablename('hs_cars_info')." where `store_id`='$uniacid' order by id desc limit 1");
        $jsonaaa = json_decode($result['json_set'],true);
        //var_dump($jsonaaa);exit;
        if($_W['ispost']){
            $data=array();
            //var_dump($_GPC['modules']);exit;
            $modules=[1=>['m'=>$_GPC['modules'],'p'=>1],2=>['m'=>$_GPC['modules'],'p'=>2],3=>['m'=>$_GPC['modules'],'p'=>3]];
            $data['json_set']=json_encode($modules);
            $data['store_id']=$_W['uniacid'];
            $data['create_time'] = time();
            $result=pdo_fetch("SELECT `id` FROM ".tablename('hs_cars_info')." where `store_id`='$uniacid' order by id desc limit 1");
            //var_dump($result);exit;
            if($result['id']){
                $res=pdo_update('hs_cars_info',$data,array('id'=>$result['id']));
            }else{
                $res=pdo_insert('hs_cars_info',$data);
            }
            $this->success('修改成功','other/index/modules');
            
        }
        include $this->template();
    }
    /*
    * 客服中心
     */
    public function service()
    {
        global $_W;
        global $_GPC;
        $uniacid=$_W['uniacid'];
        load()->func('tpl');
        $result=pdo_fetch("SELECT * FROM ".tablename('hs_cars_info')." where `store_id`='$uniacid' order by id desc limit 1");
        if($_W['ispost']){
            $data=array();
            $service_switch = $_GPC['service_switch']=='1'?'1':'2';
            $data['service_icon']=$_GPC['service_icon'];
            $data['service_switch']=$service_switch;
            $data['store_id']=$_W['uniacid'];
            $data['create_time'] = time();
            $result=pdo_fetch("SELECT `id` FROM ".tablename('hs_cars_info')." where `store_id`='$uniacid' order by id desc limit 1");
            if($result['id']){
                $res=pdo_update('hs_cars_info',$data,array('id'=>$result['id']));
            }else{
                $res=pdo_update('hs_cars_info',$data,array('id'=>$result['id']));
            }
            $this->success('修改成功','other/index/service');
        }
        include $this->template();
    }
    /**
     * 获取轮播
     */
    public function getSlide(){
        global $_W,$_GPC;
        //if($_W['isajax']){
            $query = load()->object('query');
            $list = $query->from('hs_cars_image')
                ->select(['id','url','product_id','sort'])
                ->where(['admin_id' => $_W['uniacid']])
                ->orderby('sort', 'DESC')
                ->limit($_GPC['num'] ?: 3)
                ->getall();
            if($list){
                foreach ($list as $k => $v){
                    $list[$k]['url'] = tomedia($v['url']);
                }
                echo  json_encode($list);
            }else{
                echo '';
            }
            exit();
       // }
    }

    /**
     * 修改幻灯片
     * @return mixed|string
     */
    public function editSlide(){
        global $_W,$_GPC;
        if($_GPC['data'] || $_GPC['delete']){
            foreach ($_GPC['data'] as $v){
                if(isset($v['id'])){
                    $res=pdo_update('hs_cars_image',$v,array('id'=>$v['id'],'admin_id' => $_W['uniacid']));
                }else{
                    $v['admin_id'] = $_W['uniacid'];
                    $v['create_time'] = $_SERVER['REQUEST_TIME'];
                    $res=pdo_insert('hs_cars_image',$v);
                }
            }
            if(is_array($_GPC['delete'])){
                pdo_delete('hs_cars_image', array('id' => $_GPC['delete'],'admin_id' => $_W['uniacid']));
            }
            echo json_encode(['message' => '保存成功','code' => '200']);
            exit;
        }
        echo json_encode(['message' => '数据不能为空', 'code' => '400']);
        exit;
    }


    /**
     * 获取轮播
     */
    public function getProductType(){
        global $_W,$_GPC;
        //if($_W['isajax']){
        $query = load()->object('query');
        $list = $query->from('hs_cars_product_type')
            ->select(['id','name','img','sort'])
            ->where(['store_id' => $_W['uniacid']])
            ->orderby('sort', 'DESC')
            ->limit($_GPC['num'] ?: 4)
            ->getall();
        if($list){
            foreach ($list as $k => $v){
                $list[$k]['img'] = tomedia($v['img']);
            }
            echo  json_encode($list);
        }else{
            echo '';
        }
        exit();
    }
//}

    /**
     * 修改产品类型
     * @return mixed|string
     */
    public function editProductType(){
        global $_W,$_GPC;
        if($_GPC['data'] || $_GPC['delete']){
            foreach ($_GPC['data'] as $v){
                if(isset($v['id'])){
                    $res=pdo_update('hs_cars_product_type',$v,array('id'=>$v['id'],'store_id' => $_W['uniacid']));
                }else{
                    $v['store_id'] = $_W['uniacid'];
                    $v['create_time'] = $_SERVER['REQUEST_TIME'];
                    $res=pdo_insert('hs_cars_product_type',$v);
                }
            }
            if(is_array($_GPC['delete'])){
                pdo_delete('hs_cars_product_type', array('id' => $_GPC['delete'],'store_id' => $_W['uniacid']));
            }
            echo json_encode(['message' => '保存成功','code' => '200']);
            exit;
        }
        echo json_encode(['message' => '数据不能为空', 'code' => '400']);
        exit;
    }

    /**
     * 获取产品
     */
    public function getProduct(){
        global $_W,$_GPC;
        //if($_W['isajax']){
        $query = load()->object('query');
        $list = $query->from('hs_cars_product')
            ->select(['id','name','title','price','small_image','sort'])
            ->where(['store_id' => $_W['uniacid']])
            ->orderby('sort', 'DESC')
            ->limit($_GPC['num'] ?: 4)
            ->getall();
        if($list){
            foreach ($list as $k => $v){
                $list[$k]['small_image'] = tomedia($v['small_image']);
            }
            echo  json_encode($list);
        }else{
            echo '';
        }
        exit();
    }
    /**
     * 修改产品
     * @return mixed|string
     */
    public function editProduct(){
        global $_W,$_GPC;
        if($_GPC['data'] || $_GPC['delete']){
            foreach ($_GPC['data'] as $v){
                if(isset($v['id'])){
                    $res=pdo_update('hs_cars_product',$v,array('id'=>$v['id'],'store_id' => $_W['uniacid']));

                }else{
                    $v['store_id'] = $_W['uniacid'];
                    $v['create_time'] = $_SERVER['REQUEST_TIME'];
                    $res=pdo_insert('hs_cars_product',$v);
                }
            }
            if(is_array($_GPC['delete'])){
                pdo_delete('hs_cars_product', array('id' => $_GPC['delete'],'store_id' => $_W['uniacid']));
            }
            echo json_encode(['message' => '保存成功','code' => '200']);
            exit;
        }
        echo json_encode(['message' => '数据不能为空', 'code' => '400']);
        exit;
    }
    /**
     * 获取资讯
     */
    public function getNews(){
        global $_W,$_GPC;
        $query = load()->object('query');
        $list = $query->from('hs_cars_news')
            ->select(['id','title','image'])
            ->where(['store_id' => $_W['uniacid']])
            ->orderby('id', 'DESC')
            ->limit($_GPC['num'] ?: 4)
            ->getall();
        if($list){
            foreach ($list as $k => $v){
                $list[$k]['image'] = tomedia($v['image']);
            }
            echo  json_encode($list);
        }else{
            echo $list;
        }
        exit();
    }
    /**
     * 修改资讯
     * @return mixed|string
     */
    public function editNews(){
        global $_W,$_GPC;
        if($_GPC['data'] || $_GPC['delete']){
            foreach ($_GPC['data'] as $v){
                if(isset($v['id'])){
                    $res=pdo_update('hs_cars_news',$v,array('id'=>$v['id'],'store_id' => $_W['uniacid']));
                }else{
                    $v['store_id'] = $_W['uniacid'];
                    $v['create_time'] = $_SERVER['REQUEST_TIME'];
                    $res=pdo_insert('hs_cars_news',$v);
                }
            }
            if(is_array($_GPC['delete'])){
                pdo_delete('hs_cars_news', array('id' => $_GPC['delete'],'store_id' => $_W['uniacid']));
            }
            echo json_encode(['message' => '保存成功','code' => '200']);
            exit;
        }
        echo json_encode(['message' => '数据不能为空', 'code' => '400']);
        exit;
    }

    /**
     * 选择模板
     */
    public function editModules(){
        global $_W,$_GPC;
        if(!$_GPC['modules']){
            echo json_encode(['message' => 'modules is null', 'code' => '400']);
            exit;
        }
        $res=pdo_update('hs_cars_info',['modules' => $_GPC['modules']],array('store_id' => $_W['uniacid']));
        echo json_encode(['message' => 'succ','code' => '200']);
        exit;
    }

    /**
     * 获取模板
     */
    public function getModules(){
        global $_W;
        $query = load()->object('query');
        $modules = $query->from('hs_cars_info')
            ->select(['modules'])
            ->where(['store_id' => $_W['uniacid']])
            ->get();
        if($modules){
            echo  json_encode(['modules'=>$modules['modules']]);
        }else{
            echo  json_encode(['modules'=>'base']);
        }
        exit();
    }
    /*
    * 选择模板
     */
    public function template_list()
    {
        global $_W,$_GPC;
        $uniacid=$_W['uniacid'];
        load()->func('tpl');
        $result=pdo_fetch("SELECT * FROM ".tablename('hs_cars_info')." where `store_id`='$uniacid' order by id desc limit 1");
        if($_W['ispost']){
            $data=array();
            $data['modules']=$_GPC['modules'];
            $data['store_id']=$_W['uniacid'];
            $result=pdo_fetch("SELECT `id` FROM ".tablename('hs_cars_info')." where `store_id`='$uniacid' order by id desc limit 1");
            if($result['id']){
                $res=pdo_update('hs_cars_info',$data,array('id'=>$result['id']));
            }else{
                $res=pdo_insert('hs_cars_info',$data);
            }
            $this->success('修改成功','other/index/template_list');
        }
        include $this->template();
    }
}
?>